﻿using System;
using System.Web;
using System.Web.Mvc;
using DotNet.Mvc;
using XNWebModel;
using XNWebService;
using DotNet.Utility;

namespace XNWebAPI.Controllers
{
    public class UserController : JsonController
    {
        private readonly UserService service = new UserService();

        [HttpPost]
        public ActionResult Register(UserModel model)
        {
            return Json(service.Register(model));
        }

        [HttpPost]
        public ActionResult ExistsAccount(string account)
        {
            return Json(service.ExistsAccount(account));
        }

        [HttpPost]
        public ActionResult ModifyPassword(string account, string oldPassword, string newPassword)
        {
            return Json(service.ModifyPassword(account, oldPassword, newPassword));
        }

        [HttpPost]
        public ActionResult ModifyWeiXinOperId(string account, string weiXinOperId)
        {
            return Json(service.ModifyWeiXinOperId(account, weiXinOperId));
        }

        /// <summary>
        /// 删除OpenId
        /// </summary>
        /// <param name="account"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult DelWeiXinOpenId(string account)
        {
            return Json(service.DelWeiXinOpenId(account));
        }

        [HttpPost]
        public ActionResult ResetPassword(string account, string newPassword)
        {
            return Json(service.ResetPassword(account, newPassword));
        }

        [HttpPost]
        public ActionResult Login(string account, string password)
        {
            return Json(service.Login(account, password));
        }

        /// <summary>
        /// 密文密码登陆
        /// </summary>
        /// <param name="account">帐号</param>
        /// <param name="encryptpassword">密文密码</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult LoginWithEncrypt(string account, string encryptpassword)
        {
            return Json(service.LoginWithEncrypt(account, encryptpassword));
        }

        [HttpPost]
        public ActionResult Logout(string account)
        {
            return Json(service.Logout(account));
        }

        [HttpPost]
        public ActionResult GetModel(string account)
        {
            var result = service.GetModel(account);
            var r = result.Success 
                ? Json(new EntityMessage(BoolMessage.True, result.Data)) 
                : Json(result);
            r.Settings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Include;
            return r;
        }

        /// <summary>
        /// 通过微信OpenId取用户信息
        /// </summary>
        /// <param name="weiXinOpenId"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetModelByOpenId(string weiXinOpenId)
        {
            var result = service.GetModelByOpenId(weiXinOpenId);
            var r = result.Success
                ? Json(new EntityMessage(BoolMessage.True, result.Data))
                : Json(result);
            r.Settings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Include;
            return r;
        }
    }
}